<?php

/**
 * 客栈申请
 */
class applyAction extends globalAction {
    public function index() {
        $this->display();
    }
    
    public function agreement() {
        $this->display();
    }
    public function getChildType() {
        $typeId = intval($_GET['typeId']);
        if(empty($typeId)) die();
            
        //客栈类型
        $InnTypeDao = D('InnType');
        $InnTypeList = $InnTypeDao
            ->field($InnTypeDao->_map_flip)
            ->where(array(
                $InnTypeDao->_map['menuId'] => 606,
                $InnTypeDao->_map['parent'] => $typeId,
            ))
            ->order($InnTypeDao->_map['order'])
            ->select();
        die(json_encode(makearray($InnTypeList, 'id', 'name')));
    }
    
    public function details() {
        //客栈类型
        $InnTypeDao = D('InnType');
        $InnTypeList = $InnTypeDao
            ->field($InnTypeDao->_map_flip)
            ->where(array(
                $InnTypeDao->_map['menuId'] => 606,
                $InnTypeDao->_map['parent'] => 0,
            ))
            ->order($InnTypeDao->_map['order'])
            ->select();
        $this->assign('InnTypeList',makearray($InnTypeList, 'id', 'name'));

        //客栈等级
        $InnClassDao = D('InnClass');
        $InnClassList = $InnClassDao
            ->field($InnClassDao->_map_flip)
            ->where(array( $InnClassDao->_map['menuId'] => 598 ))
            ->order($InnClassDao->_map['order'])
            ->select();
        $this->assign('InnClassList',makearray($InnClassList, 'id', 'name'));
        
        //客栈设施
        $InnFacilityDao = D('InnFacility');
        $InnFacilityList = $InnFacilityDao
            ->field($InnFacilityDao->_map_flip)
            ->where(array( $InnFacilityDao->_map['menuId'] => 599 ))
            ->order($InnFacilityDao->_map['order'])
            ->select();
        $this->assign('InnFacilityList',makearray($InnFacilityList, 'id', 'name'));
        
        //客栈主题
        $InnSubjectDao = D('InnSubject');
        $InnSubjectList = $InnSubjectDao
            ->field($InnSubjectDao->_map_flip)
            ->where(array( $InnSubjectDao->_map['menuId'] => 613 ))
            ->order($InnSubjectDao->_map['order'])
            ->select();
        $this->assign('InnSubjectList',makearray($InnSubjectList, 'id', 'name'));

        //合作方式
        $InnCooperationDao = D('InnCooperation');
        $InnCooperationList = $InnCooperationDao
            ->field($InnCooperationDao->_map_flip)
            ->where(array( $InnCooperationDao->_map['menuId'] => 600 ))
            ->order($InnCooperationDao->_map['order'])
            ->select();
        $this->assign('InnCooperationList',makearray($InnCooperationList, 'id', 'name'));

        $this->display();
    }
    
    public function contact() {
        $innid = cookie('innadd');
        if(empty($innid))
            parent::_message('error', '非法入口，请按照步骤添加客栈信息！', C('TMPL_PARSE_STRING.__KZURL__') . '/apply/details');
        $innDao = D('Inn');
        $inn = $innDao->field($innDao->_map_flip)->find($innid);
        if(!empty($inn['uid'])) {
            cookie('innadd', null);
            parent::_message('error', '非法入口，请按照步骤添加客栈信息！', C('TMPL_PARSE_STRING.__KZURL__') . '/apply/details');
        }
        $this->assign('innid', $innid);
        $this->display();
    }
    
    public function submit() {
        $innDao = D('Inn');
        if($innDao->create()) {
            if($_POST['detailsSubmit']) {
                //数据额外处理
                $innDao->facility = ','.implode(',', $_POST['facility']);
                if(!empty($_POST['trueType'])) $innDao->type = $_POST['trueType'];
                //执行添加
                $innid = $innDao->add();
                if(false !== $innid) {
                    cookie('innadd', $innid);
                    redirect( C('TMPL_PARSE_STRING.__KZURL__') . '/apply/contact' );
                } else {
                    parent::_message('error', '提交失败！请联系管理员！', $_SERVER['HTTP_REFERER'], 3);
                }
            } elseif($_POST['contactSubmit']) {
                $ret = $innDao->save();
                if(false !== $ret) {
                    cookie('innadd', null);
                    parent::_message('success', '您的资料已提交，请耐心等待审核！', C('TMPL_PARSE_STRING.__KZURL__'));
                } else {
                    parent::_message('error', '提交失败！请联系管理员！', $_SERVER['HTTP_REFERER'], 3);
                }
            }
        } else {
            parent::_message('error', implode('<br />', $innDao->getError()), $_SERVER['HTTP_REFERER'], 3);
        }
    }
}